package net.lab1024.sa.base.module.support.traffic.service;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.base.common.code.SystemErrorCode;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.module.support.traffic.constant.CommandTypeEnum;
import net.lab1024.sa.base.module.support.traffic.constant.StatusEnum;
import net.lab1024.sa.base.module.support.traffic.domain.dto.MqttCommandDTO;
import net.lab1024.sa.base.module.support.traffic.domain.dto.MqttResponseDTO;
import net.lab1024.sa.base.module.support.traffic.domain.form.TrafficLaneModeForm;
import net.lab1024.sa.base.module.support.traffic.domain.form.TrafficSetFlashtimeForm;
import net.lab1024.sa.base.module.support.traffic.domain.form.TrafficSetIntervalForm;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.stereotype.Service;

/**
 * 交通控制服务
 */
@Slf4j
@Service
public class TrafficControlService {

    @Resource
    private MqttClientService mqttClientService;

    /**
     * 设置车道模式
     */
    public ResponseDTO<MqttResponseDTO> setLaneMode(TrafficLaneModeForm form) {
        try {
            MqttCommandDTO commandDTO = new MqttCommandDTO();
            commandDTO.setRequestId(mqttClientService.generateRequestId());
            commandDTO.setCommand(CommandTypeEnum.SET_LANE_MODE.getCode());
            commandDTO.setTargetMode(form.getTargetMode());
            commandDTO.setTimestamp(System.currentTimeMillis());

            MqttResponseDTO response = mqttClientService.publishCommand(form.getDeviceRoad(), form.getDeviceId(), commandDTO);
            if (StatusEnum.SUCCESS.getCode().equals(response.getStatus())) {
                return ResponseDTO.ok(response);
            }
            return ResponseDTO.error(SystemErrorCode.SYSTEM_ERROR, response.getMessage());
        } catch (MqttException e) {
            log.error("设置车道模式失败", e);
            return ResponseDTO.error(SystemErrorCode.SYSTEM_ERROR, "MQTT通信失败: " + e.getMessage());
        } catch (Exception e) {
            log.error("设置车道模式异常", e);
            return ResponseDTO.error(SystemErrorCode.SYSTEM_ERROR, "设置车道模式失败: " + e.getMessage());
        }
    }

    /**
     * 设置心跳间隔
     */
    public ResponseDTO<MqttResponseDTO> setHeartbeatInterval(TrafficSetIntervalForm form) {
        try {
            MqttCommandDTO commandDTO = new MqttCommandDTO();
            commandDTO.setRequestId(mqttClientService.generateRequestId());
            commandDTO.setCommand(CommandTypeEnum.SET_INTERVAL.getCode());
            commandDTO.setInterval(String.valueOf(form.getInterval()));
            commandDTO.setTimestamp(System.currentTimeMillis());

            MqttResponseDTO response = mqttClientService.publishCommand(form.getDeviceRoad(), form.getDeviceId(), commandDTO);
            if (StatusEnum.SUCCESS.getCode().equals(response.getStatus())) {
                return ResponseDTO.ok(response);
            }
            return ResponseDTO.error(SystemErrorCode.SYSTEM_ERROR, response.getMessage());
        } catch (MqttException e) {
            log.error("设置心跳间隔失败", e);
            return ResponseDTO.error(SystemErrorCode.SYSTEM_ERROR, "MQTT通信失败: " + e.getMessage());
        } catch (Exception e) {
            log.error("设置心跳间隔异常", e);
            return ResponseDTO.error(SystemErrorCode.SYSTEM_ERROR, "设置心跳间隔失败: " + e.getMessage());
        }
    }

    /**
     * 设置闪烁时间
     */
    public ResponseDTO<MqttResponseDTO> setFlashtime(TrafficSetFlashtimeForm form) {
        try {
            MqttCommandDTO commandDTO = new MqttCommandDTO();
            commandDTO.setRequestId(mqttClientService.generateRequestId());
            commandDTO.setCommand(CommandTypeEnum.SET_FLASHTIME.getCode());
            commandDTO.setFlashtime(String.valueOf(form.getFlashtime()));
            commandDTO.setTimestamp(System.currentTimeMillis());

            MqttResponseDTO response = mqttClientService.publishCommand(form.getDeviceRoad(), form.getDeviceId(), commandDTO);
            if (StatusEnum.SUCCESS.getCode().equals(response.getStatus())) {
                return ResponseDTO.ok(response);
            }
            return ResponseDTO.error(SystemErrorCode.SYSTEM_ERROR, response.getMessage());
        } catch (MqttException e) {
            log.error("设置闪烁时间失败", e);
            return ResponseDTO.error(SystemErrorCode.SYSTEM_ERROR, "MQTT通信失败: " + e.getMessage());
        } catch (Exception e) {
            log.error("设置闪烁时间异常", e);
            return ResponseDTO.error(SystemErrorCode.SYSTEM_ERROR, "设置闪烁时间失败: " + e.getMessage());
        }
    }
}

